#include "animationcontrolwidget.h"
#include <QPainter>
#include <QMouseEvent>

AnimationControlWidget::AnimationControlWidget(QWidget *parent) :
    QWidget(parent)
  , m_nNumFrames(0)
  , m_nCurrFrame(0)
  , m_bGrabbed(false)
{
    setDisabled(true);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimer()));
}

bool AnimationControlWidget::isPlaying() const
{
    return m_timer.isActive();
}

void AnimationControlWidget::play()
{
    m_timer.start(30);
}

void AnimationControlWidget::stop()
{
    m_timer.stop();
}

void AnimationControlWidget::rewind()
{
    m_nCurrFrame = 0;
    emit currentFrameChanged(m_nCurrFrame);
    update();
}

void AnimationControlWidget::goToEnd()
{
    m_nCurrFrame = m_nNumFrames - 1;
    emit currentFrameChanged(m_nCurrFrame);
    update();
}

void AnimationControlWidget::stepBackward()
{
    if(m_nCurrFrame > 0)
    {
        --m_nCurrFrame;
        emit currentFrameChanged(m_nCurrFrame);
        update();
    }
}

void AnimationControlWidget::stepForward()
{
    ++m_nCurrFrame;

    if(m_nCurrFrame > m_nNumFrames - 1)
    {
        m_nCurrFrame = 0;
    }

    emit currentFrameChanged(m_nCurrFrame);
    update();
}

void AnimationControlWidget::togglePlayback()
{
    if(isPlaying())
    {
        stop();
    }
    else
    {
        play();
    }
}

void AnimationControlWidget::resizeEvent(QResizeEvent *)
{
    updateFrameRects();
}

void AnimationControlWidget::setNumFrames(int numFrames)
{
    m_nNumFrames = numFrames;
    if(m_nNumFrames > 0)
    {
        setEnabled(true);
    }

    updateFrameRects();
}

void AnimationControlWidget::setCurrentFrame(int cf)
{
    if(m_nCurrFrame == cf) return;
    m_nCurrFrame = cf;
    emit currentFrameChanged(m_nCurrFrame);
    updateFrameRects();
}

void AnimationControlWidget::onTimer()
{
    stepForward();
}

void AnimationControlWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect(rect(), Qt::white);

    if(!m_nNumFrames)
    {
        return;
    }

    QRectF r = rect();
    float fwidth = r.width()/(float)m_nNumFrames;

    painter.save();
    QFont fnt = painter.font();
    fnt.setPixelSize(6);
    painter.setFont(fnt);

    for(int i=0;i<m_nNumFrames;i++)
    {
        QRectF fr = m_frames[i].rect;
        QColor color = i%2 ? Qt::white : Qt::gray;
        painter.fillRect(fr, color);

        QRectF textRect = fr;
        textRect.moveCenter(QPoint(textRect.center().x(), rect().height()/1.5f));
        painter.drawText(textRect, Qt::AlignCenter, QString("%1").arg(m_frames[i].frameIndex));
    }

    QRectF frameMarkerRect = QRectF(fwidth*(float)m_nCurrFrame, 0, fwidth, rect().height()/2);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(Qt::red);
    painter.drawRect(frameMarkerRect.adjusted(2, 2, -2, -2));
    painter.restore();
}

void AnimationControlWidget::mousePressEvent(QMouseEvent *pEvent)
{

}

void AnimationControlWidget::mouseMoveEvent(QMouseEvent *pEvent)
{
}

void AnimationControlWidget::mouseReleaseEvent(QMouseEvent *pEvent)
{
}

void AnimationControlWidget::updateFrameRects()
{
    m_frames.clear();
    if(!m_nNumFrames)
    {
        return;
    }

    QRectF r = rect();
    float fwidth = r.width()/(float)m_nNumFrames;

    for(int i=0;i<m_nNumFrames;i++)
    {
        QRectF frameRect = QRectF(fwidth*(float)i, 0, fwidth, rect().height()/2);
        Frame frame;
        frame.frameIndex = i;
        frame.rect = frameRect;
        m_frames.append(frame);
    }

    update();
}
